package com.ytjj.qmyx.admin.service.imService.impl;

import com.ytjj.qmyx.admin.mapper.ImSessionInfoMapper;
import com.ytjj.qmyx.admin.mapper.ImUserBindInfoMapper;
import com.ytjj.qmyx.admin.model.ImCustomerLabel;
import com.ytjj.qmyx.admin.model.ImUserBindInfo;
import com.ytjj.qmyx.admin.model.dto.ImUserBindInfoDto;
import com.ytjj.qmyx.admin.model.vo.ImUserBindInfoVO;
import com.ytjj.qmyx.admin.service.imService.ImCustomerLabelService;
import com.ytjj.qmyx.admin.service.imService.ImUserBindInfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

@Service
public class ImUserBindInfoServiceImpl implements ImUserBindInfoService {

    @Resource
    private ImUserBindInfoMapper imUserBindInfoMapper;

    @Autowired
    private ImCustomerLabelService imCustomerLabelService;

    @Resource
    private ImSessionInfoMapper imSessionInfoMapper;

    /**
     *  插入由客服填写的客户信息
     * @param imUserBindInfoVO
     * @return
     */
    @Override
    public Integer insertCustomerMessage(ImUserBindInfoVO imUserBindInfoVO) {
        ImUserBindInfoDto customerMessage = imUserBindInfoMapper.selectOneBySessionId(imUserBindInfoVO.getCustomerId());
        Integer count = 0;
//        if (null == customerMessage){
//            imUserBindInfoVO.setCreateTime(new Date());
//            imUserBindInfoVO.setUpdateTime(new Date());
//            count = imUserBindInfoMapper.insertSelective(imUserBindInfoVO);
//            //同步增加客户标签数量
//            imCustomerLabelService.updateLabelUseNum(imUserBindInfoVO.getUserLabel());
//        }else {
        imUserBindInfoVO.setUserTel(StringUtils.trim(imUserBindInfoVO.getUserTel()));
        imUserBindInfoMapper.updateOne(imUserBindInfoVO);
        imUserBindInfoVO.setUpdateTime(new Date());
        imUserBindInfoVO.setId(customerMessage.getId());
        imUserBindInfoVO.setQuestionType(null);
        imUserBindInfoVO.setSessionStatus(null);
        count = imUserBindInfoMapper.updateOne(imUserBindInfoVO);
        if (null == customerMessage.getUserLabel()) {
            imCustomerLabelService.updateLabelUseNum(imUserBindInfoVO.getUserLabel());
        } else if (null != customerMessage.getUserLabel() && null != imUserBindInfoVO.getUserLabel()) {
            ImCustomerLabel imCustomerLabel = imCustomerLabelService.selectOne(customerMessage.getUserLabel());
            if (null != imCustomerLabel) {
                //同步更新标签使用次数
                imCustomerLabelService.downLabelUseNum(customerMessage.getUserLabel());
                imCustomerLabelService.updateLabelUseNum(imUserBindInfoVO.getUserLabel());
            }
        }
//        }
        //同步更新该会话的服务类型
        if (null != imUserBindInfoVO.getSessionId()){
            imSessionInfoMapper.updateQuestionTypeBySessionId(imUserBindInfoVO.getSessionId(),imUserBindInfoVO.getQuestionType());
        }
        return count;
    }

    /**
     *  删除客户信息
     * @param imUserBindInfo
     * @return
     */
    @Override
    public Integer deleteCustomerMessage(ImUserBindInfo imUserBindInfo) {
        Integer count = imUserBindInfoMapper.deleteByPrimaryKey(imUserBindInfo.getId());
        return count;
    }

    /**
     *  查询客户信息
     * @param
     * @return
     */
    @Override
    public ImUserBindInfoDto selectCustomerMessage(ImUserBindInfoVO imUserBindInfoVO) {
        //查询该会话的客户资料
        ImUserBindInfoDto imUserBindInfoDto =imUserBindInfoMapper.selectBySessionId(imUserBindInfoVO.getCustomerId());
        ImUserBindInfoDto imUserBindInfoRes = imUserBindInfoMapper.selectOne(imUserBindInfoVO.getSessionId());
        if (null == imUserBindInfoDto || imUserBindInfoRes==null){
            return null;
        }
        //如果该该会话的客户资料的解决状态未修改过,展示的解决状态为空
        imUserBindInfoDto.setSolveStatus(null == imUserBindInfoRes.getSolveStatus() ? null : imUserBindInfoRes.getSolveStatus());
        //如果该该会话的客户资料的服务类型未修改过,展示的服务类型为空
        imUserBindInfoDto.setQuestionType(null == imUserBindInfoRes.getQuestionType() ? null : imUserBindInfoRes.getQuestionType());
        imUserBindInfoDto.setId(imUserBindInfoRes.getId());
        //如果查询到的最新客服为空则返回空
        return imUserBindInfoDto;
    }

    @Override
    public String selectCustomerName(String customerId) {
        ImUserBindInfoDto imUserBindInfoRes = imUserBindInfoMapper.selectBySessionId(customerId);
        if (imUserBindInfoRes!=null){
            return imUserBindInfoRes.getUserName();
        }
        return null;
    }
}
